import updateValue

class policyIteration(updateValue):
    def __init__(self, policy_matrix, env, action_dict, delta,episode, V):
        super().__init__(policy_matrix, env, action_dict)
        self.delta = delta
        self.episode = episode
        self.V = V

    def run(self):
        for step in range(self.episode):
            while True:
                V_old = self.V.copy()
                self.V = self.update_State_value(self.V)
                if max([(abs(self.V[V_value] - V_old[V_value])) for V_value in range(len(self.V))]) <= self.delta:
                    break
            
            self.update_policy_matrix(self.V)
        return self.V, self.policy_matrix